﻿/*
    Autor : Roger Medeiros
    Data  : 01/02/2012
    Site  : www.interacaosistemas.com.br
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace i9PgAdmin.Core
{
    public class VacuumAnalyze
    {
        public Conexao conexao { get; set; }

        public VacuumAnalyze()
        {
            this.conexao = new Conexao();
        }

        public string IniciaProcesso()
        {
            try
            {
                Environment.SetEnvironmentVariable("PGPASSWORD", this.conexao.Senha);
                Environment.SetEnvironmentVariable("PGDATABASE", this.conexao.Database);
                Environment.SetEnvironmentVariable("PGHOST", this.conexao.Servidor);
                Environment.SetEnvironmentVariable("PGPORT", this.conexao.Porta.ToString());
                Environment.SetEnvironmentVariable("PGUSER", this.conexao.Usuario);

                string BackupString = "-z -f -d " + this.conexao.Database;

                var proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName = this.conexao.CaminhoBin + "\\vacuumdb.exe";
                proc.StartInfo.Arguments = BackupString;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
                proc.Close();


                return "";
            }
            catch (Exception ex)
            {
                throw new Exception("Um erro ocorreu ao efetuar a Manutenção.\n\nErro : " + ex.Message);
            }
        }
    }
}
